package com.firemoss.magicbus.messaging
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	import mx.messaging.MessageAgent;
	import mx.messaging.events.*;
	
	public class AbstractMessageAgentMediator implements IMessageAgentMediator
	{
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;

		protected var converter : IMessageConverter
		
		private var _disconReqEvent : String
		public function set disconnectionRequestedEvent( eventName : String ) : void
		{
			if ( _disconReqEvent ) 
			{
				dispatcher.removeEventListener( eventName, disconnectionRequested )
			}
			
			_disconReqEvent = eventName
			
			dispatcher.addEventListener( eventName, disconnectionRequested, false, 0, true )
		}
				
		private var _messageAgent : MessageAgent
		public function set messageAgent( messageAgent : MessageAgent ) : void
		{
			if ( _messageAgent )
			{
				_messageAgent.removeEventListener( ChannelEvent.CONNECT, channelConnectHandler )
				_messageAgent.removeEventListener( ChannelEvent.DISCONNECT, channelConnectHandler )
				_messageAgent.removeEventListener( ChannelFaultEvent.FAULT, channelFaultHandler )
				_messageAgent.removeEventListener( MessageAckEvent.ACKNOWLEDGE, ackHandler )
				_messageAgent.removeEventListener( MessageFaultEvent.FAULT, messageFaultHandler )
			}
			
			_messageAgent = messageAgent

			if ( _messageAgent )
			{
				_messageAgent.addEventListener( ChannelEvent.CONNECT, channelConnectHandler, false, 0, true )
				_messageAgent.addEventListener( ChannelEvent.DISCONNECT, channelConnectHandler, false, 0, true )
				_messageAgent.addEventListener( ChannelFaultEvent.FAULT, channelFaultHandler, false, 0, true )
				_messageAgent.addEventListener( MessageAckEvent.ACKNOWLEDGE, ackHandler, false, 0, true )
				_messageAgent.addEventListener( MessageFaultEvent.FAULT, messageFaultHandler, false, 0, true )
			}
		}
		public function get messageAgent() : MessageAgent
		{
			return _messageAgent;
		}

		public function set bodyProperty( value : String ) : void
		{
			_bodyProperty = value
		}
		public function get bodyProperty() : String 
		{
			return _bodyProperty
		}
		private var _bodyProperty : String

		public function set bodyProperties( value : Array ) : void
		{
			_bodyProperties = value
		}
		public function get bodyProperties() : Array 
		{
			return _bodyProperties
		}
		private var _bodyProperties : Array
		
		public function set connectionStateChangeEvent( value : String ) : void
		{
			_connectionStateChangeEvent = value
		}
		public function get connectionStateChangeEvent() : String 
		{
			return _connectionStateChangeEvent
		}
		private var _connectionStateChangeEvent : String

		public function set acknowledgeEvent( value : String ) : void
		{
			_acknowledgeEvent = value
		}
		public function get acknowledgeEvent() : String 
		{
			return _acknowledgeEvent
		}
		private var _acknowledgeEvent : String

		public function set channelFaultEvent( value : String ) : void
		{
			_channelFaultEvent = value
		}
		public function get channelFaultEvent() : String 
		{
			return _channelFaultEvent
		}
		private var _channelFaultEvent : String

		public function set faultEvent( value : String ) : void
		{
			_faultEvent = value
		}
		public function get faultEvent() : String 
		{
			return _faultEvent
		}
		private var _faultEvent : String

		// NOTIFICATION LISTENERS
		
		private function disconnectionRequested( event : Event ) : void
		{
			if ( messageAgent )
			{
				messageAgent.disconnect()
			}
		}
		
		// AGENT EVENTS 
		
		private function channelConnectHandler( event : Event ) : void
		{
			var note : Object = converter.convertChannelConnectEvent( event )
			
			if ( note )
			{
				dispatch( note )
			}
		}
		
		private function channelFaultHandler( event : Event ) : void
		{
			var note : Object = converter.convertChannelFault( event )
			
			if ( note )
			{
				dispatch( note )
			}
		}
		
		private function ackHandler( event : Event ) : void
		{
			var note : Object = converter.convertAcknowledgeEvent( event )
			
			if ( note )
			{
				dispatch( note )
			}
		}
		
		private function messageFaultHandler( event : Event ) : void
		{
			var note : Object = converter.convertMessageFault( event )
			
			if ( note )
			{
				dispatch( note )
			}
		}
		
		// IMessageAgentMediator stubs
		
		public function dispatch( event : Object ) : void
		{
			// NO-OP:  implement in concrete classes
		}


	}
}